home *** CD-ROM | disk | FTP | other *** search
- Path: in1.uu.net!interaccess!usenet
- From: brianmcg@interaccess.com (Brian V. McGroarty)
- Newsgroups: comp.lang.c
- Subject: Re: Capturing useful return values from the parallel port
- Date: 19 Mar 1996 14:47:52 GMT
- Organization: Internet Squire
- Message-ID: <4imheo$12b@nntp.interaccess.com>
- References: <4ilur8$hgi@lyra.csx.cam.ac.uk>
- Reply-To: brianmcg@interaccess.com
- NNTP-Posting-Host: d46-isdn.nhe.interaccess.com
- X-Newsreader: Internet Squire 1.20
-
- I assume that you want the system to cleanly handle situations where
- multiple buttons
- are pressed; logically, a number from 1 to 4 can't contain the state of all
- buttons
- simultaneously.
-
- Try something like this. It's not pretty, but it's fairly straightforward,
- and your compiler should turn it into a series of bit tests, which is much
- faster than shifting and anding, and it's smaller than a look-up table,
- which would probably have been my next suggestion.
-
- #define MASK_A (1<<1) /* bit position of button 1 is "1"=2 */
- #define MASK_B (1<<3) /* bit position of button 2 is "3"=8 */
- #define MASK_C (1<<5) /* etc. */
- #define MASK_D (1<<7)
-
- #define CHECKMASK( X, Y, Z ) ((keys&(X)) ? (Y) : (Z))
- unsigned pressedKey( void )
- {
- char keys;
-
- keys= inportb( PORTID );
-
- return(
- CHECKMASK( MASK_A, 1,
- CHECKMASK( MASK_B, 2,
- CHECKMASK( MASK_C, 3,
- CHECKMASK( MASK_D, 4, 0 )
- )
- )
- );
- }
-
- > I am building a small routine which is designed to return
- > values 1-4 from a four bbutton resposne box plugged into
- > the parallel port. The buttons normally return bits 0
- > through 3 but I am having problems converting these,
- > elegantly, into the required values.
-
- > I have been using a routine which returns the product of
- > an XOR bit transformation. This is fine for two buttons,
- > but obviously not for four.
-
- > The routine that does this looks like this:
-
- > @define PORTID 0x379
-
- > unsigned pressedKey(void)
- > {
- >
- > return ((inportb(PORTID) >> 6) ^1);
- >
- > }
-
-
-
-
- >If you are able to help me with this I would appreciate contact through
- >either my
- >Cambridge email or London email addresses. The London one is
-
- Done, copied to group.
-
-
- ---
- Brian Valters McGroarty -- brianmcg@bix.com
- phone/fax (847) 439-7714
-